package com.fenghongzhang.module_main.glide.nice_pic
import android.content.Context

import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SimpleItemAnimator

import com.fenghongzhang.lib_net.R
import com.fenghongzhang.lib_net.model.Image
import com.fenghongzhang.module_main.glide.pic.Info
import com.fenghongzhang.module_main.glide.pic.PhotoView



/**
 *@author Wcj
 *@description
 *@date 2022/7/5 14:50
 */
class NineGridPicsView : FrameLayout {
    private val mParentView: ViewGroup
    private val mRvPic: RecyclerView
    private var mPicsList: List<Image>? = null
    private var onItemClickListener: ((Int, Int, List<String>, List<Info>) -> Unit)? = null
    private var onItemClickWithThumbnailListener: ((Int, Int, List<Image>, List<Info>) -> Unit)? = null
    private val mItemDecoration by lazy {
        SpacesItemDecoration(4)
    }

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        mParentView = LayoutInflater.from(context).inflate(R.layout.layout_nine_grid_pics_view, this) as ViewGroup
        mRvPic = mParentView.findViewById(R.id.rv_pic_9)

    }

    //设置图片显示列表
    fun setPicsList(list: List<Image>) {
        this.mPicsList = list
    }

    //设置点击事件
    fun setOnItemClickListener(l: (Int, Int, List<String>, List<Info>) -> Unit) {
        this.onItemClickListener = l
    }
    fun setOnItemImageClickListener(l: (Int, Int, List<Image>, List<Info>) -> Unit) {
        this.onItemClickWithThumbnailListener = l
    }

    //初始化View //增加点击空白处的事件
    fun initView(position: Int, onClickEmptyArea: (() -> Unit)? = null) {
        if (mPicsList == null) {
            return
        }
        val hasSize = !mPicsList!!.any { it.imageWidth == 0 || it.imageHeight == 0 }
        val urlList = mPicsList!!.map { it.url }


        val lg = object : GridLayoutManager(
            context,
            //1-3图片判断
            if (hasSize && mPicsList!!.size == 1) 1 else if (hasSize && mPicsList!!.size == 2) 2 else 3,
            RecyclerView.VERTICAL,
            false
        ) {
            //禁止滑动
            override fun canScrollVertically(): Boolean {
                return false
            }

            override fun canScrollHorizontally(): Boolean {
                return false
            }
        }
        mRvPic.layoutManager = lg
        (mRvPic.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false
        val ninePicsAdapter = NinePicsAdapter(mPicsList!!) {
            val imageInfo = mutableListOf<Info>()
            repeat(lg.childCount) { pos ->
                val iv = lg.findViewByPosition(pos) as ImageView
                imageInfo.add(PhotoView.getImageViewInfo(iv))
            }
            onItemClickListener?.invoke(position, it, urlList, imageInfo)
            onItemClickWithThumbnailListener?.invoke(position, it, mPicsList!!, imageInfo)
        }

        mRvPic.removeItemDecoration(mItemDecoration)
        mRvPic.addItemDecoration(mItemDecoration)
        mRvPic.adapter = ninePicsAdapter
        repeat(mRvPic.childCount) {
            PhotoView.getImageViewInfo(mRvPic.getChildAt(it) as ImageView)
            mRvPic.getChildAt(it).setOnClickListener {
            }
        }
//        RecyclerMarginClickHelper.setOnMarginClickListener(mRvPic) {
//            onClickEmptyArea?.invoke()
//        }

    }
}